package com.qingke.easyjava.jdbc.conn;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Jdbcconn {

	public static void main(String[] args) {
		System.out.println("huanyin");
		    Connection  conn = null;
				try {
					try {
						Class.forName("com.mysql.jdbc.Driver");
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//String url = "jdbc:mysql://localhost:3306/qingke";
					String url = getProperty("db.url");
					String u = getProperty("db.username");
					String p = getProperty("db.password");
					System.out.println(u);
					conn = DriverManager.getConnection(url,u,p);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					
						try {
							if(conn!=null){
							conn.close();
							}
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
			
	}
	private static String getProperty(String key){
		Properties  prop = new  Properties();
		try {
			prop.load(new FileInputStream("jdbc.properties"));
			return prop.getProperty(key);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	 public static Connection getConnection(){
		  String driver = getProperty("db.driver");
		  Connection  conn = null;
		  try {
			Class.forName(driver);
			 String url = getProperty("db.url");
			  String u = getProperty("db.username");
			  String p = getProperty("db.password");
			  
			  try {
				 conn = DriverManager.getConnection(url,u,p);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return conn;
	  }

}
